package corej.course.basic;

public class TestInsertDelete {
    static int[] data = {2, 5, 7, 6, 3, 8, 9, 4};

    static int index = 8;

    static void insert(int pos, int value) {
        if (index == data.length)
            expand();
        for (int i = index; i > pos; i--) {
            data[i] = data[i - 1];
        }
        data[pos] = value;
        index++;
    }

    static void delete(int pos) {
        index--;
        for (int i = pos; i < index; i++) {
            data[i] = data[i + 1];
        }
    }

    static void expand() {
        int[] a = new int[data.length * 2];
        System.arraycopy(data, 0, a, 0, data.length);
        data = a;
    }

    static void print() {
        for (int i = 0; i < index; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        insert(2, 1);// 2 5 1 7 6 3 8 9 4
        print();
        delete(1); // 2 1 7 6 3 8 9 4
        print();
    }
}